﻿using BookStore.Solution.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookStore.Solution.BLL
{
    public class EmployeeManager
    {
        /*
         * Insert Employee to database
         */
        public string Insert(Employee t)
        {
            Employee Employee = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                try
                {
                    Employee = ctx.Employees.Where(a => a.EmployeeID == t.EmployeeID).FirstOrDefault<Employee>();
                    if (Employee == null)
                    {
                        ctx.Employees.Add(t);
                        if (ctx.SaveChanges() > 0)
                        {
                            SystemLogManager.Insert("Thêm nhân viên: " + t);
                            return "Thêm thành công";
                        }
                        else
                            return "Thêm thất bại";
                    }
                    else
                        return "Trùng khóa chính";
                }
                catch (Exception e)
                {
                    return e.Message;
                }
            }
        }

        /**
         * Update employee
         */
        public string Update(Employee t)
        {
            Employee entity = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                try
                {
                    entity = ctx.Employees.Where(a => a.EmployeeID == t.EmployeeID).FirstOrDefault<Employee>();
                    if (entity != null)
                    {
                        entity.EmployeeName = t.EmployeeName;
                        entity.Birthday = t.Birthday;
                        entity.Gender = t.Gender;
                        entity.IndentityCard = t.IndentityCard;
                        entity.Address = t.Address;
                        entity.Email = t.Email;
                        entity.PhoneNumber = entity.PhoneNumber;
                        entity.Position = entity.Position;
                        entity.Status = entity.Status;
                        ctx.Entry(entity).State = System.Data.Entity.EntityState.Modified;
                        if (ctx.SaveChanges() > 0)
                        {
                            t = entity;
                            SystemLogManager.Insert("Cập nhật nhân viên: " + t);
                            return "Cập nhật thành công";
                        }
                        else
                            return "Cập nhật thất bại";

                    }
                    else
                        return "Mã nhân viên không tồn tại";
                }
                catch (Exception e)
                {
                    return e.Message;
                }
            }
        }

        /*
         * Delete employee
         */
        public string Delete(Employee t)
        {
            Employee entity = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                try
                {
                    Account account = ctx.Accounts.Where(e => e.EmployeeID == t.EmployeeID).FirstOrDefault<Account>();
                    if (account == null)
                    {
                        entity = ctx.Employees.Where(a => a.EmployeeID == t.EmployeeID).FirstOrDefault<Employee>();
                        if (entity != null)
                        {
                            entity.Status = "Sa thải";
                            
                            ctx.Entry(entity).State = System.Data.Entity.EntityState.Modified;
                            if (ctx.SaveChanges() > 0)
                            {
                                SystemLogManager.Insert("Xóa nhân viên: " + t);
                                return "Xóa thành công";
                            }
                            else
                                return "Xóa thất bại";
                        }
                        else
                            return "Mã nhân viên không tồn tại";
                    }
                    else
                        return "Tồn tại tài khoản của nhân viên này";
                }
                catch (Exception e)
                {
                    return e.Message;
                }
            }
        }

        /*
         * Tìm kiếm theo tên chức vụ
         */
        public DataTable SearchbyName(string name)
        {
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                var EmployeeList = (from a in ctx.Employees where a.EmployeeName.Contains(name) select a).ToList<Employee>();
                DataTable dt = CreateTable(EmployeeList);
                return dt;
            }
        }

        /*
         * Tìm kiếm theo mã chức vụ
         */
        public DataTable SearchByID(string id)
        {
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                var EmployeeList = (from a in ctx.Employees where a.EmployeeID == id select a).ToList<Employee>();
                DataTable dt = CreateTable(EmployeeList);
                return dt;
            }
        }

        public DataTable SearchByPosition(int PosiotionID)
        {
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                var EmployeeList = (from a in ctx.Employees where a.PositionID == PosiotionID select a).ToList<Employee>();
                DataTable dt = CreateTable(EmployeeList);
                return dt;
            }
        }

        private DataTable CreateTable(List<Employee> EmployeeList)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("EmployeeID", Type.GetType("System.String"));
            dt.Columns.Add("EmployeeName", Type.GetType("System.String"));
            dt.Columns.Add("Birthday", typeof(DateTime));
            dt.Columns.Add("Gender", typeof(bool)); 
            dt.Columns.Add("IndentityCard", Type.GetType("System.String"));
            dt.Columns.Add("Address", Type.GetType("System.String"));
            dt.Columns.Add("Email", Type.GetType("System.String"));
            dt.Columns.Add("PhoneNumber", Type.GetType("System.String"));
            dt.Columns.Add("PositionID", Type.GetType("System.Int32"));
            dt.Columns.Add("Status", Type.GetType("System.String"));
            foreach (Employee e in EmployeeList)
            {
                dt.LoadDataRow(new object[] { e.EmployeeID, e.EmployeeName, e.Birthday, e.Gender, e.IndentityCard, e.Address, e.Email, e.PhoneNumber, e.PositionID, e.Status }, false);
            }
            return dt;
        }
        public DataTable GetAll()
        {
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                List<Employee> EmployeeList = (from e in ctx.Employees where e.Status != "Sa thải"
                                               select e).ToList<Employee>();
                DataTable dt = CreateTable(EmployeeList);
                return dt;
            }
        }
    }
}